package 代码随想录.数组.最小覆盖子串;

import sun.nio.cs.ext.MacHebrew;

import java.io.PrintWriter;

/**lc76. 最小覆盖子串
 * link：https://leetcode-cn.com/problems/minimum-window-substring/
 *
 * todo
 * 有一个死循环，一直没找到。。。
 * 感觉逻辑没问题
 * --11.30
 *
 * @author ChenweiLin
 * @create 2021-11-30 16:20
 */
public class minimumWindowSubstring {
    public static void main(String[] args) {
        System.out.println(minWindow("ADOBECODEBANC", "ABC"));
    }
    public static String minWindow(String s, String t) {
        char[] sChar = s.toCharArray();//大串
        char[] tChar = t.toCharArray();//小串
        //滑动窗口
        int left = 0;
        int right = 0;
        StringBuffer result = new StringBuffer();
        int len = Integer.MAX_VALUE;
        for (; right < sChar.length; right++) {
            while (check(left, right, sChar, tChar)){
                if(len >= right - left + 1){//如果是符合要求的更小子串
                    len = right - left + 1;//更新len
                    result = mark(left, right, sChar);//记录下此时中间的String
                    left++;//左窗口往前移一格进行while循环的检验判断

                }
                System.out.println("left:" + left);
                System.out.println("right:" + right);
            }
        }

        return result.toString();
    }

    private static StringBuffer mark(int left, int right, char[] sChar) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = left; i <= right; i++) {
            stringBuffer.append(sChar[i]);
        }
        return stringBuffer;
    }


    public static boolean check(int left,int right ,char[] sChar,char[] tChar){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = left; i <= right; i++) {
            stringBuffer.append(sChar[i]);
        }
        String s = stringBuffer.toString();
        for (char c : tChar) {
            if(s.indexOf(c) == -1)
                return false;
        }

        return true;
    }

}
